package commonClasses;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.Explanation.IDFExplanation;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.store.FSDirectory;

public class IDF {
	
	public static void main(String []args) throws IOException, ParseException{
		FileReader fr = new FileReader(
				"J:\\BC3_IMT_Training\\PSIMI.txt");

		BufferedReader fbr = new BufferedReader(fr);
		String str = fbr.readLine();

		PrintStream ps = new PrintStream("J:\\BC3_IMT_Training\\IDF.txt");
		while (str != null) {
			String dict = str.substring(8);
			String did = str.substring(0, 7);
			String d[]=dict.split(" ");

			
			//System.out.println(dict);
			float idf = IndexSearch(d);
			ps.println(did+" "+idf);
			str=fbr.readLine();
		}

	}
	
	public static float IndexSearch(String[] input) throws IOException, ParseException{
		
		FSDirectory index = FSDirectory.open(new File(
				"J:\\BC3_IMT_Training\\Index"));

		ArrayList<Term> t = new ArrayList<Term>();
		for(int i=0;i<input.length;i++){
			Term term = new Term(input[i]);
			t.add(term);
		}
		
		IndexReader indexReader = IndexReader.open(index);
		IndexSearcher searcher = new IndexSearcher(indexReader);
		
		Similarity sim = new DefaultSimilarity(); 
		IDFExplanation idf=sim.idfExplain(t , searcher);
		float i = idf.getIdf();
		System.out.println(i);
		return i;

	}
}
